package com.client.service;

import com.client.bean.UserInfo;
import com.client.dbdao.UserInfoClientDao;
import com.data.bean.Account;
import com.data.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 用户管理
 *
 * @author:HingLo
 * @create 2017-10-12 9:16
 **/
@Service
public class UserInfoClientService {
    @Autowired
    private UserInfoClientDao userInfoClientDao;


    public List<?> userLoginService(String username, String password) {
        //如果字符串是空的，就直接返回
        if (!CheckDataUtils.stringUtils(username, password)) {
            return null;
        }


        username = username.trim();
        password = password.trim();
        String type = LoginMethodUtils.accountType(username);
        List<?> list = userInfoClientDao.userLoginDao(type, username, Md5CodeUtils.md5(password));
        if (list.size() == 1) {
            Object[] o1 = (Object[]) list.get(0);
            String id = (String) o1[0];
            String token = Md5CodeUtils.md5(id + new Date().toString());
            //更新token
            if (userInfoClientDao.updateHeaderDao(5, id, token)) {
                return userInfoClientDao.userLoginDao(type, username, Md5CodeUtils.md5(password));
            }
        }

        return list;

    }

    public boolean userRegiesterService(String id, String username, String password) {
        username = username.trim();
        password = password.trim();
        Account account = new Account();
        account.setState(FinalStringUtils.STATE_ACTIVE);
        account.setIstate(FinalStringUtils.STATE_ACTIVE);
        account.setTypes(FinalStringUtils.CLIENT);
        account.setId(id);
        account.setPassword(Md5CodeUtils.md5(password));
        account.setEmail(username);
        account.setUsername(username);
        account.setCtime(new Date());

        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setUid(id);
        userInfo.setToken(Md5CodeUtils.md5(id + new Date().toString()));
        userInfo.setCtime(new Date());
        userInfo.setUsername(DatabaseIdUtils.getDataIdOne());

        return userInfoClientDao.saveMassageDao(account, userInfo);
    }

    public boolean updateUserInfoService(String id, Object value, int type) {
        id = id.trim();
        if (type == 0) {
            if (CheckDataUtils.stringUtils(id) && value != null) {
                //修改头像
                return userInfoClientDao.updateHeaderDao(type, id, ((CommonsMultipartFile) value).getBytes());
            }
        } else {
            if (CheckDataUtils.stringUtils(id, (String) value)) {
                return userInfoClientDao.updateHeaderDao(type, id, value);
            }
        }
        return false;
    }

    /**
     * 更新密码
     *
     * @param id
     * @param password
     * @return
     */
    public boolean updateUserInfoPasswordService(String id, String password) {

        id = id.trim();
        password = password.trim();
        return userInfoClientDao.updatePasswordDao(id, password);


    }

    public boolean chenkUserInfoTokenService(String id, String token) {
        if (!CheckDataUtils.stringUtilsID(id, token)) {
            return false;
        }

        id = id.trim();
        token = token.trim();
        List<?> list = userInfoClientDao.chenkUserInfoTokenDao(id, token);
        return list != null && !list.isEmpty();
    }

    public byte[] lookUserInfoHeaderService(String uid) {
        if (CheckDataUtils.stringUtils(uid)) {
            uid = uid.trim();
            List<UserInfo> list = userInfoClientDao.lookUserInfoHeaderDao(uid);
            if (!list.isEmpty()) {
                byte header[] = list.get(0).getHeader();
                if (header != null) {
                    return header;
                }
            }
        }
        return FileResumeUtils.readMinIcon("default", "default.png");
    }

    /**
     * 检测账号是否已经被占用
     *
     * @param username
     * @return
     */
    public boolean checkEmailService(String username) {
        if (CheckDataUtils.stringUtils(username) && CheckDataUtils.checkEmail(username)) {
            username = username.trim();
            String res = LoginMethodUtils.accountType(username);
            return userInfoClientDao.checkEmailDao(res, username);
        }
        return false;
    }

    /**
     * 忘记密码
     *
     * @param email
     * @return
     */
    public boolean forgetPasswordCheckEmailService(String email) {
        if (CheckDataUtils.stringUtils(email) && CheckDataUtils.checkEmail(email)) {
            email = email.trim();
            List<?> list = userInfoClientDao.forgetPasswordCheckEmailDao(email);
            if (!list.isEmpty())
                return true;
        }
        return false;
    }

    /**
     * 通过邮箱来重置密码
     *
     * @param email
     * @param password
     * @return
     */
    public boolean settingPasswordService(String email, String password) {

        if (CheckDataUtils.checkEmail(email)) {
            email = email.trim();
            password = password.trim();
            return userInfoClientDao.settingPasswordDao(email, Md5CodeUtils.md5(password));
        }
        return false;
    }

    public List<?> lookUserInfoTokenService(String id, String token) {
        if (!CheckDataUtils.stringUtilsID(id, token)) {
            return null;
        }

        id = id.trim();
        token = token.trim();
        return userInfoClientDao.lookUserInfoTokenDao(id, token);
    }

    /**
     * 三方登录
     *
     * @param uid
     * @param token
     * @return
     */
    public boolean otherLoginService(String uid, String token) {
        Account account = new Account();
        account.setState(FinalStringUtils.STATE_ACTIVE);
        account.setIstate(FinalStringUtils.STATE_ACTIVE);
        account.setTypes(FinalStringUtils.CLIENT);
        account.setId(uid);
        account.setPassword(Md5CodeUtils.md5("123456"));
        account.setEmail("");
        String s = DatabaseIdUtils.getDataIdOne();
        account.setUsername(s);
        account.setCtime(new Date());

        UserInfo userInfo = new UserInfo();
        userInfo.setId(uid);
        userInfo.setUid(uid);
        userInfo.setToken(token);
        userInfo.setCtime(new Date());
        userInfo.setUsername(s);
        return userInfoClientDao.saveMassageDao(account, userInfo);
    }

    public boolean loginOutService(String uid) {
        return userInfoClientDao.updateloginOutDao(uid);
    }
}